﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PersonalHealth.Data.Model;
using PersonalHealth.Data.IService;
using PersonalHealth.Data.Service;
using Telerik.Windows.Controls.Charting;
using System.Windows.Controls.Primitives;

namespace PersonHealth.View
{
    /// <summary>
    /// BloodSugarList.xaml 的交互逻辑
    /// </summary>
    public partial class BloodSugarList : Page
    {
        int pageIndex = 1;
        int pageCount = 16;
        int year = DateTime.Now.Year;
        int month = DateTime.Now.Month;

        public BloodSugarList()
        {
            InitializeComponent();

            setChart();
            bindChartData();
        }

        private void ToggleButton_Checked(object sender, RoutedEventArgs e)
        {
            if (this.IsLoaded)
            {
                if (tubBtn.IsChecked == true)
                {
                    libBtn.IsChecked = false;
                    stP1.Visibility = Visibility.Collapsed;
                    stP2.Visibility = Visibility.Visible;
                    stP3.Visibility = Visibility.Visible;

                    year = DateTime.Now.Year;
                    month = DateTime.Now.Month;

                    bindChartData();
                }
            }
        }

        private void libBtn_Checked(object sender, RoutedEventArgs e)
        {
            if (this.IsLoaded)
            {
                if (libBtn.IsChecked == true)
                {
                    tubBtn.IsChecked = false;
                    stP2.Visibility = Visibility.Collapsed;
                    stP1.Visibility = Visibility.Visible;
                    stP3.Visibility = Visibility.Collapsed;

                    pageIndex = 1;
                    bindListData();
                }
            }
        }


        private void BloodGlucose_checked(object sender, RoutedEventArgs e)
        {
            foreach (var i in rc.SeriesMappings)
            {
                if (i.LegendLabel == "血糖(mmol/L)")
                {
                    i.SeriesDefinition.Visibility = (sender as ToggleButton).IsChecked.Value ? SeriesVisibility.Visible : SeriesVisibility.Hidden;
                }
            }
        }



        private void setChart()
        {
            rc.DefaultView.ChartArea.AxisX.Visibility = Visibility.Collapsed;

            rc.DefaultView.ChartLegend.HeaderTemplate = this.Resources["BOTemplate"] as DataTemplate;
            rc.DefaultView.ChartLegend.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("Transparent"));
            rc.DefaultView.ChartLegend.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("Transparent"));
            rc.DefaultView.ChartLegend.LegendItemMarkerShape = MarkerShape.SquareRounded;
            rc.DefaultView.ChartLegend.Width = 160;
            rc.DefaultView.ChartLegend.Margin = new Thickness(-10, -20, 10, 0);

            #region mapping
            rc.SeriesMappings.Clear();

            SeriesMapping mapping = new SeriesMapping();
            mapping.LegendLabel = "血糖(mmol/L)";
            SplineSeriesDefinition def = new SplineSeriesDefinition();
            def.ShowItemToolTips = true;
            def.ShowItemLabels = true;
            def.ItemToolTipFormat = "测量时间: #DATAITEM.MeasureTime{yyyy年MM月dd日 HH:mm}\r\n测量值: #Y{N0}";
            def.SeriesItemTooltipStyle = Application.Current.FindResource("LineChartTooltipStyle") as Style;
            mapping.SeriesDefinition = def;
            mapping.ItemMappings.Add(new ItemMapping("BloodGlucose", DataPointMember.YValue));
            rc.SeriesMappings.Add(mapping);
            #endregion
        }

        private void Previous_Click(object sender, MouseButtonEventArgs e)
        {
            if (pageIndex == 1)
                return;
            pageIndex--;
            bindListData();
        }

        private void Next_Click(object sender, MouseButtonEventArgs e)
        {

            if (this.DataContext != null && pageIndex == (this.DataContext as BGModel).Pages)
                return;
            pageIndex++;
            bindListData();
        }

        private void bindListData()
        {
            MeasureUser user = AppData.User;
            IMeasure_BGService ser = new Measure_BGService();

            int pages = 0;
            int records = 0;
            Func<Measure_BG, DateTime>[] orderby = new Func<Measure_BG, DateTime>[] { new Func<Measure_BG, DateTime>(c => c.MeasureTime) };
            bool[] isdesc = new bool[] { true };
            IList<Measure_BG> objs = ser.Find<DateTime>(c => c.UserId == user.Id, orderby, isdesc, pageIndex, pageCount, out pages, out records);
            if (objs != null)
            {
                gv.ItemsSource = objs;

                BGModel model = new BGModel();
                model.PageIndex = pageIndex;
                model.PageCount = pageCount;
                model.Pages = pages;
                model.Records = records;
                this.DataContext = model;
            }
        }

        private void bindChartData()
        {
            IMeasure_BGService ser = new Measure_BGService();
            Func<Measure_BG, bool> func = new Func<Measure_BG, bool>(c => c.UserId == AppData.User.Id && c.MeasureTime.Year == year && c.MeasureTime.Month == month);
            Func<Measure_BG, DateTime>[] orderby = new Func<Measure_BG, DateTime>[] { new Func<Measure_BG, DateTime>(c => c.MeasureTime) };
            bool[] isdesc = new bool[] { false };
            var list = ser.Find<DateTime>(func, orderby, isdesc, 1, 100);
            rc.ItemsSource = list;

            tbDate.Text = string.Format("{0}年{1}月", year, month);
        }

        private void Previous_Click1(object sender, MouseButtonEventArgs e)
        {
            DateTime date = new DateTime(year, month, 1).AddMonths(-1);
            year = date.Year;
            month = date.Month;

            bindChartData();
        }

        private void Next_Click1(object sender, MouseButtonEventArgs e)
        {
            DateTime date = new DateTime(year, month, 1).AddMonths(1);
            year = date.Year;
            month = date.Month;
            bindChartData();
        }
       
    }

    public class BGModel
    {
        public int Records { get; set; }
        public int PageCount { get; set; }

        public int PageIndex { get; set; }

        public int Pages { get; set; }
    }
}
